package com.geeyuu.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import com.geeyuu.android.GeeApplication;
import com.geeyuu.android.R;
import com.geeyuu.android.service.DataKey;
import com.geeyuu.android.service.ProtocolCommand;
import com.geeyuu.android.utils.AndroidUtil;
import com.geeyuu.android.utils.CommonUtil;

public class RegisterActivity extends BaseActivity {

    private String currentAccount = "";
    private String currentPwd = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.TAG = "RegisterActivity";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);

        GeeApplication geeApplication = (GeeApplication) getApplication();
        geeApplication.addActivity(this);

        initTitleViews();
        initContentViews();

        startBindService();

    }
    
    @Override
    protected void onDestroy() {
        stopBindService();
        super.onDestroy();
    }

    @Override
    protected void onReceiveMsgFromService(int cmd, Bundle bundle, boolean isMsgNormal) {
        switch (cmd) {
        case ProtocolCommand.CMD_REGISTER:
            if(isMsgNormal){
                if (DataKey.R_VALUE_REGISTER_ACCOUNT_EXIST == bundle.getInt(DataKey.R_KEY_REGISTER_ACCOUNT_EXIST, 0)) { // 账号已经存在
                    cancelProgressDialog();
                    AndroidUtil.showToast(getApplicationContext(), "账号已经存在");
                } else {
                    // 发送登录请求
                    Bundle loginBundle = new Bundle();
                    loginBundle.putString(DataKey.S_KEY_LOGIN_TYPE, DataKey.S_VALUE_LOGIN_TYPE_NORMAL);
                    loginBundle.putString(DataKey.S_KEY_LOGIN_ACCOUNT, currentAccount);
                    loginBundle.putString(DataKey.S_KEY_LOGIN_PASSWORD, currentPwd);
                    loginBundle.putString(DataKey.S_KEY_IS_GUEST, DataKey.S_VALUE_REGISTER_IS_NOT_GUEST);
                    sendMsgToService(ProtocolCommand.CMD_LOGIN, loginBundle);
                }
            }else{
                cancelProgressDialog();
                AndroidUtil.showToast(getApplicationContext(), "注册失败，请重试");
            }
            break;
        case ProtocolCommand.CMD_LOGIN:
            cancelProgressDialog();
            if(isMsgNormal){
                // 跳转
                Intent intent = new Intent(RegisterActivity.this, RegisterUserInfoActivity.class);
                startActivity(intent);
                finish();
            }else{
                AndroidUtil.showToast(getApplicationContext(), "注册失败，请重试");
            }
            break;
        }
    }

    private OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.top_right_button:
                if (CommonUtil.isEmpty(currentAccount) || CommonUtil.isEmpty(currentPwd)) {
                    AndroidUtil.showToast(getApplicationContext(), "账号或者密码不能为空");
                    return;
                }
                if (currentAccount.length() > 30) {
                    AndroidUtil.showToast(getApplicationContext(), "账号长度30个字符以内");
                    return;
                }
                if (currentPwd.length() < 6 || currentPwd.length() > 30) {
                    AndroidUtil.showToast(getApplicationContext(), "密码长度6-30");
                    return;
                }
                if (!CommonUtil.isEmail(currentAccount)) {
                    AndroidUtil.showToast(getApplicationContext(), "请输入正确的邮箱");
                    return;
                }
                showProgressDialog("正在注册", "奋力注册中,请稍等...!", false);
                AndroidUtil.unlogin(RegisterActivity.this);
                // 发送注册请求
                Bundle bundle = new Bundle();
                bundle.putString(DataKey.S_KEY_REGISTER_ACCOUNT, currentAccount);
                bundle.putString(DataKey.S_KEY_REGISTER_PASSWORD, currentPwd);
                bundle.putString(DataKey.S_KEY_IS_GUEST, DataKey.S_VALUE_REGISTER_IS_NOT_GUEST);
                sendMsgToService(ProtocolCommand.CMD_REGISTER, bundle);
                break;
            case R.id.top_left_button:
                finish();
                break;
            }
        }
    };

    private void initTitleViews() {
        Button next = (Button) findViewById(R.id.top_right_button);
        next.setText("下一步");
        next.setVisibility(View.VISIBLE);
        next.setOnClickListener(mOnClickListener);

        Button topLeftButton = (Button) findViewById(R.id.top_left_button);
        topLeftButton.setVisibility(View.VISIBLE);
        topLeftButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.title_back_icon, 0, R.drawable.logo_icon47, 0);
        topLeftButton.setPadding(AndroidUtil.dip2px(this, 5), 0, 0, 0);
        ViewGroup.LayoutParams vl = topLeftButton.getLayoutParams();
        if (null == vl) {
            vl = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT);
        } else {
            vl.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            vl.height = ViewGroup.LayoutParams.FILL_PARENT;
        }
        topLeftButton.setLayoutParams(vl);
        topLeftButton.setOnClickListener(mOnClickListener);

        TextView topLeftTextView = (TextView) findViewById(R.id.top_left_text_view);
        topLeftTextView.setText("注册第一步");
        topLeftTextView.setVisibility(View.VISIBLE);
    }

    private void setEditTextPasswordMode(EditText editText, boolean isChecked) {
        if (isChecked) {
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            editText.setSelection(currentPwd.length());
        } else {
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            editText.setSelection(currentPwd.length());
        }
    }

    private void initContentViews() {
        findViewById(R.id.step_line0).setBackgroundResource(R.drawable.corners_four_blue_bg);

        final EditText etAccount = (EditText) findViewById(R.id.email);
        final EditText etPassword = (EditText) findViewById(R.id.password);
        setEditTextPasswordMode(etPassword, true);

        TextWatcher accountWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                currentAccount = s.toString();
                if (null != currentAccount && currentAccount.length() > 0) {
                    currentAccount.trim().replace(" ", "");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        };
        TextWatcher pwdWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                currentPwd = s.toString();
                if (null != currentPwd && currentPwd.length() > 0) {
                    currentPwd.trim().replace(" ", "");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        etAccount.addTextChangedListener(accountWatcher);
        etPassword.addTextChangedListener(pwdWatcher);

        CheckBox checkbox = (CheckBox) findViewById(R.id.show_pwd_checkbox);
        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setEditTextPasswordMode(etPassword, isChecked);
            }
        });

    }

}
